package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.dongdong.mapper.TbGoodsMapper;
import com.dongdong.model.TbGoods;
import com.dongdong.model.TbGoodsExample;
import com.dongdong.page.PageParams;
import com.dongdong.service.GoodsDescService;
import com.dongdong.service.GoodsService;
import com.dongdong.service.ItemService;
import com.dongdong.util.IdWorker;
import com.dongdong.util.JWTUtil;
import com.dongdong.vo.GoodsDescVO;
import com.dongdong.vo.GoodsSPUVO;
import com.dongdong.vo.ItemVO;
import com.dongdong.vo.SellerVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper goodsMapper;

    @Autowired
    private GoodsDescService goodsDescService;

    @Autowired
    private ItemService itemService;

    @Autowired
    private JWTUtil jwtUtil;

    @Override
    public int InsertGoodsSPU(GoodsSPUVO goodsSPUVO, String autherizetion) {
        TbGoods goods = new TbGoods();
        BeanUtils.copyProperties(goodsSPUVO, goods);

//        IdWorker idWorker = new IdWorker();//利用雪花算法生成唯一且不为空的id
//        goods.setId(idWorker.nextId());

        //从token中解析获取当前登录用户信息  shiro不能直接获取因为session不能服务之间共享
        if (StrUtil.isEmpty(autherizetion)) {//判断是否为空
            return 12;//token失效
        }
        Claims claims = null;
        try {
            claims = jwtUtil.parseJwt(autherizetion);//解析token
        } catch (Exception e) {
            return 12;//token失效
        }
        String subject = claims.getSubject();//获取subject对象
        SellerVO sellerVO = JSON.parseObject(subject, SellerVO.class);//用vo接收一下解析好的用户信息


        goods.setSellerId(sellerVO.getSellerId());//直接获取  并且赋值
        int i = goodsMapper.insertSelective(goods);
        return i;
    }

    @Override
    public PageParams<GoodsSPUVO> getGoodsList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbGoods> tbGoods = goodsMapper.selectByExample(null);
        PageInfo<TbGoods> info = new PageInfo<>(tbGoods);

        List<GoodsSPUVO> collect = tbGoods.stream().map(goodslist -> {
            GoodsSPUVO vo = new GoodsSPUVO();
            BeanUtils.copyProperties(goodslist, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageParams<>(info.getPageNum(), info.getPageSize(), collect, info.getTotal(), info.getPages());
    }

    @Override
    public List<GoodsSPUVO> getStatus(String status) {
        TbGoodsExample example = new TbGoodsExample();
        if (status != null) {
            example.createCriteria().andAuditStatusEqualTo(status);
        }
        List<TbGoods> tbGoods = goodsMapper.selectByExample(example);
        List<GoodsSPUVO> collect = tbGoods.stream().map(goods -> {
            GoodsSPUVO vo = new GoodsSPUVO();
            BeanUtils.copyProperties(goods, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int delteGoods(Long[] arr) {
        TbGoodsExample example = new TbGoodsExample();
        example.createCriteria().andIdIn(Arrays.asList(arr));
        int i = goodsMapper.deleteByExample(example);
        return i;
    }

    @Override
    public GoodsSPUVO FindGoodsName(GoodsSPUVO goodsSPUVO) {
        if (goodsSPUVO.getGoodsName() == null) {
            return null;
        }
        TbGoodsExample example = new TbGoodsExample();
        example.createCriteria().andGoodsNameLike("%" + goodsSPUVO.getGoodsName() + "%");
        List<TbGoods> tbGoods = goodsMapper.selectByExample(example);
        GoodsSPUVO vo = null;
        for (TbGoods tbGood : tbGoods) {
            vo = new GoodsSPUVO();
            BeanUtils.copyProperties(tbGood, vo);
        }
        return vo;
    }

    @Override
    public int UpdateAuditStatus(Long[] arr) {
        int i = 0;
        for (Long aLong : arr) {
            i = goodsMapper.UpdateAuditStatus(aLong);
        }
        if (i <= 0) {
            return 0;
        }
        return i;
    }


    @Override
    public GoodsSPUVO getGoodsVO(Long id) {
        TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
        GoodsSPUVO goodsvo = new GoodsSPUVO();
        BeanUtils.copyProperties(tbGoods,goodsvo);

        //查询详情表
        GoodsDescVO descVO = goodsDescService.getGoodsDescById(id);

        //SKU
        List<ItemVO> itemvolist = itemService.getItemById(id);

        goodsvo.setGoodsDescVO(descVO);
        goodsvo.setItemvo(itemvolist);
        return goodsvo;
    }
}
